package com.woniu.his.controller;

import com.woniu.his.entity.DocterInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

/**
 * @ClassName LoginController
 * @Description 登录验证
 * @Author li
 * @Date 2019/11/27 0027 16:41
 * @Version V1.0
 **/
@Controller
public class LoginController {

//    日志信息
    private Logger logger= LoggerFactory.getLogger(LoginController.class);

    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String tologin(){
        return "login";
    }

    /**
     *@描述 根据不同的角色跳转到不同的页面
     *@参数
     *@返回值
     *@创建人  li
     *@创建时间  2019/11/27 0027
     *@修改人和其它信息
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(String userName, String password, ModelMap map, HttpSession httpSession) {
        System.out.println("用户账号为:=>"+userName);
        System.out.println("用户密码为:=>"+password);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        map.put("userName",userName);
//        登录验证
        try{
            subject.login(token);
            DocterInfo principal = (DocterInfo) subject.getPrincipal();
            httpSession.setAttribute("DocterInfo", principal);
        }catch (ShiroException e){
            logger.info("登录异常",e);
            String message=null;
            if (e instanceof UnknownAccountException){
                message="用户不存在";
            }else if (e instanceof IncorrectCredentialsException){
                message="密码错误!";
            }else{
                message=e.getMessage();
            }
            map.put("msg",message);
            return "login";
        }catch (Exception e){
            logger.info("登录异常",e);
            map.put("msg","系统异常");
            return "login";
        }

//        如果没报错,说明登录成功,根据对应的身份跳转到对应的页面
//        获取身份信息
        if (subject.hasRole("住院部")) {
            return "redirect:inhospital/index";
        } else if (subject.hasRole("护士部")) {
            return "redirect:tao-nurse-page";
        }else if (subject.hasRole("手术部")){
            return "redirect:operation-section";
        }else if (subject.hasRole("财务部")){
            return "redirect:finance";
        } else if (subject.hasRole("挂号部")) {
            DocterInfo docterInfo=(DocterInfo)subject.getPrincipal();
            Integer docterId = docterInfo.getDocterId();
            map.put("docterId",docterId);
            return "guahao";
        } else if (subject.hasRole("门诊部")) {
            return "redirect:outpatient/index";
        }
        return "login";
    }

    /**
     *@描述 登出
     *@参数
     *@返回值
     *@创建人  li
     *@创建时间  2019/11/27 0027
     *@修改人和其它信息
     */
    @RequestMapping("loginout")
    public String loginOut(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }
}
